﻿using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using YiMeiJiaLibModel.Assembly;
using YiMeiJiaLibModel.Enums;

namespace YiMeiJiaService.Assembly
{
    /// <summary>
    /// js 调用组件
    /// </summary>
    public class EvalJs
    {
        
        public static EvalJs ExJs()
        {
            return new EvalJs();
        }

        #region 自定义内容调用
        /// <summary>
        /// 执行带返回的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="js"></param>
        /// <returns></returns>
        public async Task<T> Eval<T>(IJSRuntime JSRuntime, string js)
        {
            try
            {
                var func_ = $"_X{Guid.NewGuid().ToString("n")}X";
                var funp_ = $" function {func_}() {{   {js}   }};{func_}(); ";
                var res = await JSRuntime.InvokeAsync<T>("eval", funp_);
                return (T)res;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        /// <summary>
        /// 执行不带返回的
        /// </summary>
        /// <param name="js"></param>
        public async void EvalVoid(IJSRuntime JSRuntime, string js)
        {
            await JSRuntime.InvokeVoidAsync("eval", $"{js}"); ;
        }
        #endregion



        #region 预设应用调用
        /// <summary>
        /// weui 弹窗组件
        /// </summary>
        /// <param name="msg"></param>
        public async Task alert(IJSRuntime JSRuntime, string content, string title = "")
        {
            if (string.IsNullOrEmpty(title))
                await JSRuntime.InvokeVoidAsync("eval", $"$.alert('{content}');");
            else
                await JSRuntime.InvokeVoidAsync("eval", $"$.alert('{content}','{title}');");
        }

        public async Task Toast(IJSRuntime JSRuntime, string msg, EmToast type)
        {
            await JSRuntime.InvokeVoidAsync("eval", $"$.toast(\"{msg}\", \"{Common.GetDescription(type)}\");");
        }

        /// <summary>
        /// Toptip
        /// </summary>
        /// <param name="JSRuntime"></param>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        /// <param name="times">秒</param>
        /// <returns></returns>
        public async Task Toptip(IJSRuntime JSRuntime, string msg, EmToptip type,int times=2)
        {
            times = times * 1000;
            await JSRuntime.InvokeVoidAsync("eval", $"$.toptip('{msg}', {times}, '{Common.GetDescription(type)}');");
        }

        /// <summary>
        /// 标准通知，点击直接跳转类
        /// </summary>
        /// <param name="JSRuntime"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task NotificationStandard(IJSRuntime JSRuntime, JsNotification data)
        {
            var js = $@"
$.notification({{
  title: ""{data.title}"",
  text: ""{data.text}"",
  media: ""<img src='{data.media}'>"",
  data: ""{data.data}""
,time:{(data.time==0? 6000 : data.time)},
  onClick: function(data) {{
    window.location.href=""{data.onClick}?{data.data}"";
    $.alert(""Click"" + data);
  }},
  onClose: function(data) {{
  
  }}
}});
";
            await JSRuntime.InvokeVoidAsync("eval", $"{js}");
        }



        /// <summary>
        /// 加载中动画
        /// </summary>
        /// <param name="JSRuntime"></param>
        /// <param name="time"></param>
        /// <param name="hide"></param>
        /// <returns></returns>
        public async Task LoadMask(IJSRuntime JSRuntime,  bool hide = false)
        {
            if (hide)
                await JSRuntime.InvokeVoidAsync("eval", @"$.hideLoading();");
            else
                await JSRuntime.InvokeVoidAsync("eval", $"$.showLoading('请稍候');");
        }
        #endregion

    }

  
}
